﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace BfLib.Web.RSS
{
    public class RSSChannel
    {
        private List<RSSItem> _rssItems;

        //Mandatory channel elements
        private string _title, _link, _description;

        //Optional channel elements
        private string _language, _copyright, _managingEditor, _webMaster, _pubDate,
                       _lastBuildDate, _generator, _docs, _ttl, 
                       _rating, _skipHours, _skipDays;

        private RSSCategoryList _category;
        private RSSChannelImage _image;
        private RSSChannelCloud _cloud;
        private RSSChannelTextInput _textInput;

        #region Public accessors

        public string Description
        {
            get { return _description; }
        }

        public string Link
        {
            get { return _link; }
        }

        public string Title
        {
            get { return _title; }
        }

        public string SkipDays
        {
            get { return _skipDays; }
        }

        public string SkipHours
        {
            get { return _skipHours; }
        }

        public RSSChannelTextInput TextInput
        {
            get { return _textInput; }
        }

        public string Rating
        {
            get { return _rating; }
        }

        public RSSChannelImage Image
        {
            get { return _image; }
        }

        public string Ttl
        {
            get { return _ttl; }
        }

        public RSSChannelCloud Cloud
        {
            get { return _cloud; }
        }

        public string Docs
        {
            get { return _docs; }
        }

        public string Generator
        {
            get { return _generator; }
        }

        public RSSCategoryList Category
        {
            get { return _category; }
        }

        public string LastBuildDate
        {
            get { return _lastBuildDate; }
        }

        public string PubDate
        {
            get { return _pubDate; }
        }

        public string WebMaster
        {
            get { return _webMaster; }
        }

        public string ManagingEditor
        {
            get { return _managingEditor; }
        }

        public string Copyright
        {
            get { return _copyright; }
        }

        public string Language
        {
            get { return _language; }
        }

        #endregion

        public RSSChannel(XmlNode node)
        {
            RSSItem rssItem;
            _rssItems = new List<RSSItem>();

            XmlNode n;
            XmlNodeList nodes;

            #region Parse channel elements

            n = node.SelectSingleNode("title");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <title> element");
            else
                this._title = n.InnerXml;

            n = node.SelectSingleNode("link");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <link> element");
            else
                this._link = n.InnerXml;

            n = node.SelectSingleNode("description");
            if (n == null)
                throw new Exception("<channel> element does not contain the mandatory <description> element");
            else
                this._description = n.InnerXml;





            n = node.SelectSingleNode("language");
            if (n != null)
                this._language = n.Value;

            n = node.SelectSingleNode("copyright");
            if (n != null)
                this._copyright = n.Value;

            n = node.SelectSingleNode("managingEditor");
            if (n != null)
                this._managingEditor = n.Value;

            n = node.SelectSingleNode("webMaster");
            if (n != null)
                this._webMaster = n.Value;

            n = node.SelectSingleNode("pubDate");
            if (n != null)
                this._pubDate = n.Value;

            n = node.SelectSingleNode("lastBuildDate");
            if (n != null)
                this._lastBuildDate = n.Value;

            n = node.SelectSingleNode("category");
            this._category = new RSSCategoryList(n);

            n = node.SelectSingleNode("generator");
            if (n != null)
                this._generator = n.Value;

            n = node.SelectSingleNode("docs");
            if (n != null)
                this._docs = n.Value;


            ///TODO because cloud is in the form 
            ///<cloud domain="rpc.sys.com" port="80" path="/RPC2" registerProcedure="pingMe" protocol="soap"/>
            n = node.SelectSingleNode("cloud");
            if (n != null)
                this._cloud = new RSSChannelCloud(n);



            n = node.SelectSingleNode("ttl");
            if (n != null)
                this._ttl = n.Value;

            n = node.SelectSingleNode("image");
            this._image = new RSSChannelImage(n);

            n = node.SelectSingleNode("rating");
            if (n != null)
                this._rating = n.Value;

            n = node.SelectSingleNode("textInput");
            this._textInput = new RSSChannelTextInput(n);

            n = node.SelectSingleNode("skipHours");
            if (n != null)
                this._skipHours = n.Value;
            n = node.SelectSingleNode("skipDays");
            if (n != null)
                this._skipDays = n.Value;

            #endregion

            nodes = node.SelectNodes("item");

            if (nodes == null)
                return;
            if (nodes.Count == 0)
                return;

            foreach (XmlNode nd in nodes)
            {
                rssItem = new RSSItem(nd);
                _rssItems.Add(rssItem);
            }
        }

        public List<RSSItem> RSSItems
        {
            get
            {
                return _rssItems;
            }
        }
    }

}
